package tech.abing.spider.task.pageprocessor.amac;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.utils.TaskUtil;

public class CancelListProcessor implements PageProcessor{

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		if(!request.getUrl().contains("index_")){
			//获取总页数
			//上一页 1 下一页   第 /1页  跳转
			String totalPagesStr = doc.select("table.noBorder tr > td").text();
			int totalPages = TaskUtil.str2Int(totalPagesStr.substring(totalPagesStr.indexOf("/")+1, totalPagesStr.lastIndexOf("页")));
			
			if(totalPages > 1){
				for(int i = 2; i <= totalPages; i++){
					String url = request.getUrl() + "index_"+(i - 1)+".shtml";
					PageRequest req = context.createPageRequest(url, BlackListProcessor.class);
					req.setHeaders(request.getHeaders());
					queue.add(req);
				}
			}
		}
		
		Elements table = doc.select("div.iRight > div.newsList1,div.newsList2");
		if(table !=null && table.size()>0){
			for (Element element : table) {
				String href = element.select("div.newsName > a").attr("href");
				href = href.substring(href.lastIndexOf("/"));
				String url = request.getUrl().substring(0, request.getUrl().lastIndexOf("/"));
				url = url + href;
				
				PageRequest req = context.createPageRequest(url, CancelListDetailProcessor.class);
				
				queue.add(req);
			}
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

	

}
